﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GRC_TPE_WebApp.Controller;

namespace GRC_TPE_WebApp.View.Private.Client
{
    public partial class DisplayClient : System.Web.UI.Page
    {
        int idClient = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            /// tentative de récupération de l'id
            if (!int.TryParse(Request.QueryString["id"], out idClient))
                /// redirection s'il y a une erreur au Parse
                Response.Redirect("~/View/Private/Home.aspx");
            else
            {
                /// la récupération à réussie
                ///Filtres des contacts du client
                this.DS_Contacts.Where = "it.ClientId == @CurrentClient";
                this.DS_Contacts.WhereParameters.Add(new Parameter() { DbType = System.Data.DbType.Int32, Name = "CurrentClient", DefaultValue = this.idClient.ToString() });

                this.DS_Communications.Where = "it.ClientId == @CurrentClient";
                this.DS_Communications.WhereParameters.Add(new Parameter() { DbType = System.Data.DbType.Int32, Name = "CurrentClient", DefaultValue = this.idClient.ToString() });

                this.DS_Businesses.Where = "it.ClientId == @CurrentClient";
                this.DS_Businesses.WhereParameters.Add(new Parameter() { DbType = System.Data.DbType.Int32, Name = "CurrentClient", DefaultValue = this.idClient.ToString() });

            }
        }

        /// <summary>
        /// Méthode déclenchée une fois la page chargée
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);

            if (this.idClient != 0)
            {
                string[] clientFileds = ClientController.GetClientFields(this.idClient);

                ///si tous les champs ont bien été retournés
                if (clientFileds.Count() == 4)
                {
                    this.displayClientName.Text = clientFileds[0];
                    this.displayClientActivity.Text = clientFileds[1];
                    this.displayClientAdress.Text = clientFileds[2];
                    this.displayPhone.Text = clientFileds[3];
                }
                else
                    /// redirection s'il y a une erreur au Parse
                    Response.Redirect(Request.Url.AbsolutePath);
            }
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le lien d'ajout d'un contact
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LB_NewContact_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/View/Private/Client/AddContact.aspx?id=" + idClient);
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le lien d'ajout d'une communication
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LB_NewCommunication_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/View/Private/Communication/AddCommunication.aspx?id=" + idClient);
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le lien d'ajout d'une affaire
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LB_NewBusiness_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/View/Private/Business/AddBusiness.aspx?id=" + idClient);
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de modification d'un contact
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_EditContact_Click(object sender, EventArgs e)
        {
            String idContact = (String)((Button)sender).CommandArgument;
            Response.Redirect("~/View/Private/Client/EditContact.aspx?id=" + idContact);
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de modification d'une communication
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_EditCommunication_Click(object sender, EventArgs e)
        {
            String idComm = (String)((Button)sender).CommandArgument;
            Response.Redirect("~/View/Private/Communication/EditCommunication.aspx?id=" + idComm);
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de modification d'une affaire
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_EditBusiness_Click(object sender, EventArgs e)
        {
            String idBusiness = (String)((Button)sender).CommandArgument;
            Response.Redirect("~/View/Private/Business/EditBusiness.aspx?id=" + idBusiness);
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de suppression
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_DeleteBusiness_Click(object sender, EventArgs e)
        {
            String idBusiness = (String)((Button)sender).CommandArgument;
            int id = int.Parse(idBusiness);
            if(BusinessController.DeleteBusiness(id) == "No error")
                Response.Redirect("DisplayClient.aspx");
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de suppression
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_DeleteContact_Click(object sender, EventArgs e)
        {
            String idContact = (String)((Button)sender).CommandArgument;
            int id = int.Parse(idContact);
            if (ContactController.DeleteContact(id) == "No error")
                Response.Redirect("DisplayClient.aspx");
        }

        /// <summary>
        /// Méthode déclenchée au clic sur le bouton de suppression
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void B_DeleteCommunication_Click(object sender, EventArgs e)
        {
            String idCommunication = (String)((Button)sender).CommandArgument;
            int id = int.Parse(idCommunication);
            if (CommunicationController.DeleteCommunication(id) == "No error")
                Response.Redirect("DisplayClient.aspx");
        }
    }
}